%!TeX program=xelatex
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{gzgbook}

%%================模式=====================
\RequirePackage{kvoptions}
\RequirePackage{etoolbox}
\RequirePackage{calc}
\SetupKeyvalOptions{family=gzg,prefix=gzg@,setkeys=\kvsetkeys}
\newcommand{\ekv}[1]{\kvsetkeys{gzg}{#1}}

%%标题
\DeclareStringOption[deftitle]{biaotistyle}
\DeclareVoidOption{deftitle}{\ekv{biaotistyle=deftitle}}
\DeclareVoidOption{official}{\ekv{biaotistyle=official}}
\DeclareVoidOption{ebooktitle}{\ekv{biaotistyle=ebooktitle}}

\DeclareDefaultOption{\PassOptionsToClass{\CurrentOption}{ctexbook}}
\ProcessKeyvalOptions*\relax
\LoadClass[UTF8,fontset=none,scheme=chinese,oneside]{ctexbook}


%%================标题格式================%%
%\usepackage{needspace}
\ifdefstring{\gzg@biaotistyle}{deftitle}{
\ctexset{
    tocdepth=2,
    secnumdepth=2,
    abstractname=摘\quad 要,
    contentsname=目\quad 录,
    part={
        tocline=\Large\CTEXifname{\CTEXthepart\ \ }{}#2,
        format=\centering\LARGE\color{biaoti},
%        name={第,部},
        aftername=\vskip 0.3em,
        pagestyle=myfancy,
    },
    chapter={
        format=\centering\Large\color{biaoti} \heiti,
        name={第,章},
        beforeskip=0ex,
        afterskip=2.5ex,
        aftername = \enspace,	
        pagestyle=myfancy
    },
    section={
        format=\color{biaoti} \kaishu,
        number=\arabic{section},
        name={,.},
        beforeskip=0.5ex,
        afterskip=0.5ex,
        aftername=\enspace,
    },
    subsection={
        format=\color{biaoti} \fangsong,
        number=\arabic{subsection},
        name={(,)},
        beforeskip=0.5ex,
        afterskip=0.5ex,
        aftername=\enspace,
    },
    subsubsection={
        format=\color{biaoti} \fangsong,
        number=\Roman{subsubsection},
        name={,.},
        beforeskip=0.5ex,
        afterskip=0.5ex,
        aftername=\enspace,
    },
    paragraph={
        runin=false,
        format+=\color{biaoti}\centering,
        afterskip=1ex,
        beforeskip=1ex,
    },
}}{\relax}
\ifdefstring{\gzg@biaotistyle}{ebooktitle}{
    \ctexset{
        tocdepth=2,
        secnumdepth=2,
        abstractname=摘\quad 要,
        contentsname=目\quad 录,
        part={
            tocline=\Large\CTEXifname{\CTEXthepart\ \ }{}#2,
            format=\centering\LARGE\color{cored},
            %        name={第,部},
            aftername=\vskip 0.3em,
            pagestyle=myfancy,
        },
        chapter={
            format=\centering\Large\color{cored} \heiti,
            name={第,章},
            beforeskip=0ex,
            afterskip=2.5ex,
            aftername = \enspace,	
            pagestyle=myfancy
        },
        section={
            format=\color{kblue} \kaishu,
            number=\arabic{section},
            name={,.},
            beforeskip=0.5ex,
            afterskip=0.5ex,
            aftername=\enspace,
        },
        subsection={
            format=\color{purple} \fangsong,
            number=\arabic{subsection},
            name={(,)},
            beforeskip=0.5ex,
            afterskip=0.5ex,
            aftername=\enspace,
        },
        subsubsection={
            format=\color{coteal} \fangsong\centering,
%            number=\Roman{subsubsection},
            name={,},
            beforeskip=0.5ex,
            afterskip=0.5ex,
            aftername=\enspace,
        },
%        paragraph={
%            runin=false,
%            format=\color{cocyan}\centering,
%            afterskip=1ex,
%            beforeskip=1ex,
%        },
}}{\relax}
\ifdefstring{\gzg@biaotistyle}{official}{
    \ctexset{
        abstractname=摘\quad 要,
        contentsname=目\quad 录,
        secnumdepth=4,
        tocdepth=4,
        chapter={
            break={},
            %        tocline=\CTEXifname{\thepart\hspace{0.5em}}{}#2,
            beforeskip=0ex,
            afterskip=4ex,
            numbering=false,
            format=\centering\songti\zihao{2}\textbf,
            aftername=0pt,
            pagestyle=myfancy
        },
        section={
            format=\heiti,
            number=\chinese{section},
            name={,、},
            aftername = \hspace{0.5em},
            afterskip=0pt,
        },
        subsection={
            format=\kaishu,
            number=\chinese{subsection},
            name={（,）},
            aftername=\hspace{0.5em},
            afterskip=0pt,
        },
        subsubsection={
            format=\fangsong,
            number=\arabic{subsubsection},
            name={,.},
            aftername=\hspace{0.5em},
            afterskip=0pt,
            tocline=\CTEXifname{\CTEXthesubsubsection\hspace{0.5em}}{}#2,
        },
        paragraph={
            format=\fangsong,
            number=\arabic{paragraph},
            name={（,）},
            aftername=\hspace{0.5em},
            afterskip=0pt~{}\par,
            tocline=\hspace{-1em}\CTEXifname{\CTEXtheparagraph\hspace{0.5em}}{}#2,
        },
    }
}{\relax}


\usepackage{titletoc}
\dottedcontents{part}[3.5em]{}{3.5em}{0pc}
\dottedcontents{chapter}[3.5em]{}{3.5em}{0.8pc}
\dottedcontents{section}[3em]{}{1.75em}{0.8pc}
\titlecontents*{subsection}[3em]{\small}{}{}{·\thecontentspage}[\quad][]

%\DeclareOption{abstract}{%%为book定制摘要
%    \makeatletter
%    \if@titlepage
%    \newenvironment{abstract}{%
%        \titlepage
%        \null\vfil
%        \@beginparpenalty\@lowpenalty
%        \begin{center}%
%            \heiti \abstractname
%            \@endparpenalty\@M
%    \end{center}\kaishu}%
%    {\par\vfil\null\endtitlepage}
%    \else
%    \newenvironment{abstract}{%
%        \if@twocolumn
%        \chapter*{\abstractname}%
%        \else
%        \small
%        \begin{center}%
%            {\heiti \abstractname\vspace{-.5em}\vspace{\z@}}%
%        \end{center}%
%        \quotation
%        \fi
%        }
%    {\if@twocolumn\else\endquotation\fi}
%    \fi
%    \makeatother
%}
%\DeclareOption{abstract}{
    \newenvironment{abstract}{\vspace*{1ex}\begin{center}
            \heiti\abstractname
        \end{center} \par\indent \kaishu }{\\\par}
%}
%\DeclareOption*{\relax}
%\ProcessOptions*\relax

\RequirePackage{myoptions}
\pagestyle{myfancy}